package com.treehouse.gui.loaderview;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;

import com.treehouse.gui.ViewControllerComponent;
import com.treehouse.gui.loaderview.viewComp.ActorScenesCellRenderer;
import com.treehouse.gui.loaderview.viewComp.ActorScenesTableModel;
import com.treehouse.service.application.IApplicationService;

public class LoaderViewController extends ViewControllerComponent implements ILoaderViewController, ActionListener{
	private JTable actorScenes;
	private JTable loadingScenes;
	private JPanel middlePan;
	private JButton selectAll;
	private JButton deselectAll;
	private JButton putToDownloadList;
	private JButton removeFromDownloadList;
	private JButton startDownload;
	private JButton stopDownload;
	private JLabel currentScene;
	private JLabel actor;
	private JLabel size;
	private JLabel downloadSpeed;
	private JLabel timeSpend;
	private JLabel estimatedTime;
	
	
	public LoaderViewController(IApplicationService applicationService) {
		super(applicationService);
		this.initialize();
		this.doLayout();
	}
	
	private void initialize(){
		this.actorScenes = new JTable();
		this.loadingScenes = new JTable();
		this.middlePan = new JPanel();
		this.selectAll = new JButton("Select all");
		this.deselectAll = new JButton("Deselect all");
		this.putToDownloadList = new JButton("Put to downloadlist");
		this.removeFromDownloadList = new JButton("Remove from downloadlist");
		this.startDownload = new JButton("Start download");
		this.stopDownload = new JButton("Stop download");
		this.currentScene = new JLabel("Current Scene: ");
		this.actor = new JLabel("Actor: ");
		this.size = new JLabel("Size: ");
		this.downloadSpeed = new JLabel("Downloadspeed: ");
		this.timeSpend = new JLabel("Time spend: ");
		this.estimatedTime = new JLabel("Estimated time left: ");
	}
	
	private void doLayout(){
		
		
		GridBagLayout gbl = new GridBagLayout();
		GridBagConstraints gbcTop = new GridBagConstraints();
		GridBagConstraints gbcMid = new GridBagConstraints();
		GridBagConstraints gbcBot = new GridBagConstraints();
		gbcTop.gridx = 0;
		gbcTop.gridy = 0;
		gbcTop.fill = GridBagConstraints.BOTH;
		gbcTop.weightx = 0.49;
		gbcTop.weighty = 0.49;
		
		gbcMid.gridx = 0;
		gbcMid.gridy = 1;
		gbcMid.fill = GridBagConstraints.BOTH;
		gbcMid.weightx = 0.02;
		gbcMid.weighty = 0.02;
		
		gbcBot.gridx = 0;
		gbcBot.gridy = 2;
		gbcBot.fill = GridBagConstraints.BOTH;
		gbcBot.weightx = 0.49;
		gbcBot.weighty = 0.49;
		
		JScrollPane actorPane = new JScrollPane(this.actorScenes);
		JScrollPane scenePane = new JScrollPane(this.loadingScenes);
		
		gbl.addLayoutComponent(actorPane, gbcTop);
		gbl.addLayoutComponent(this.middlePan, gbcMid);
		gbl.addLayoutComponent(scenePane, gbcBot);
		
		
		this.getView().setLayout(gbl);
		this.actorScenes.setModel(new ActorScenesTableModel());
		this.loadingScenes.setModel(new ActorScenesTableModel());
		this.middlePan.setLayout(new GridLayout(2, 6));
		ActorScenesTableModel scenesModel = (ActorScenesTableModel)this.loadingScenes.getModel();
		ActorScenesTableModel actorModel = (ActorScenesTableModel)this.actorScenes.getModel();
		String[] columnNamesForScenes = {"Name",
                "Rating",
                "Downloaded"};
		Object[][] dataForScenes = {
			    {"Testscene", new Double(9.99),
			     new Boolean(false)},
			     {"Blascene", new Double(9.99),
			    	 new Boolean(false)}
			};
		String[] columnNamesForLoader = {"Name",
                "Rating", "Actor",
                "Progress"};
		
		JProgressBar progress = new JProgressBar();
		progress.setValue(0);
		progress.setStringPainted(true);
		
		Object[][] dataForLoader = {
				{"Testscene", new Double(9.99),
					"TesterStar", progress
				},
				{"Blascene", new Double(0.00),
					"BlaStar", new JProgressBar(0,100)
				}
		};
		
		actorModel.setColumns(columnNamesForScenes);
		actorModel.setRows(dataForScenes);
		scenesModel.setColumns(columnNamesForLoader);
		scenesModel.setRows(dataForLoader);
		this.getView().add(actorPane);
		this.getView().add(this.middlePan);
		this.getView().add(scenePane);
		this.actorScenes.setPreferredSize(new Dimension(500, 100));
		this.actorScenes.setFillsViewportHeight(true);
		this.loadingScenes.setPreferredSize(new Dimension(500, 100));
		this.loadingScenes.setFillsViewportHeight(true);
		this.loadingScenes.setAutoCreateRowSorter(true);
		this.actorScenes.setAutoCreateRowSorter(true);
		this.middlePan.add(this.selectAll);
		this.middlePan.add(this.deselectAll);
		this.middlePan.add(this.putToDownloadList);
		this.middlePan.add(this.removeFromDownloadList);
		this.middlePan.add(this.startDownload);
		this.middlePan.add(this.stopDownload);
		this.middlePan.add(this.currentScene);
		this.middlePan.add(this.actor);
		this.middlePan.add(this.size);
		this.middlePan.add(this.downloadSpeed);
		this.middlePan.add(this.timeSpend);
		this.middlePan.add(this.estimatedTime);
		this.selectAll.addActionListener(this);
		this.deselectAll.addActionListener(this);
		this.loadingScenes.setDefaultRenderer(JProgressBar.class, new ActorScenesCellRenderer());
		
		

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();
		
		if(command.equals("Select all")){
		this.actorScenes.selectAll();
		}
		else if(command.equals("Deselect all")){
			ListSelectionModel lsm = this.actorScenes.getSelectionModel();
			lsm.clearSelection();
		}
		
	}

}

